<?php
/**
 *  This class handles all user actions 
 *	@author Alecsandru Gabriel DINU
 *  @since Jun 19, 2008
 * 
 *  @Bean(id = "userAction")
 *  @Property(name = "userManager", ref="userManager")  
 *  @Property(name = "roleManager", ref="roleManager") 
 */
class UserAction extends BaseAction {
	private $id;
	private $user;
	private $users;
	private $roles;
	private $availableRoles;
	
	private $userManager;
	private $roleManager;
	
	
	/**
	 * Sets the UserManager bean
	 * @access public
	 * @param UserManager
	 */
	public function setUserManager(UserManager $userManager) {
		$this->userManager = $userManager;
	}

	/**
	 * Sets the RoleManager bean
	 * @access public
	 * @param RoleManager
	 */
	public function setRoleManager(RoleManager $roleManager) {
		$this->roleManager = $roleManager;
	}
	
	/**
	 * Login action. Handles a user login request.
	 * @access public
	 * @return string
	 */
	public function login() {
		if ($this->getContext()->isUserLoggedIn()) {
			return "logged";
		}
		return SUCCESS;
	}
	
	public function logout() {
		$this->userManager->logout($this->getContext());		
		return SUCCESS;
	}
	
	public function doLogin() {
		if ($this->getContext()->isUserLoggedIn()) {
			return "logged";
		}
		$result = $this->userManager->login($this->getContext(), $this->user);
		if ($result) {
			return "logged";
		} else {
			$this->addErrorMessage($this->getText("login_error_bad_credentials"));
			return ERROR;
		}
	}
	
	public function register() {
		$this->user = new User();
		return SUCCESS;
	}
	
	public function save() {
		try {
			$this->userManager->save($this->user);
			$this->addSuccessMessage($this->getText("user_message_save_success"));
		} catch (ResourceAlreadyExistsException $e) {
			$this->log->error($e);
			$this->addErrorMessage($this->getText("user_message_save_error_exists"));
		} catch (BadEmailException  $e) {
			$this->log->error($e);
			$this->addErrorMessage($this->getText("user_message_save_error_bad_email"));
		} catch (Exception $e) {
			$this->log->error($e);
			$this->addErrorMessage($this->getText("user_message_save_error"));				
		}
		return SUCCESS;
	}

	public function panel() {
		if (!$this->getContext()->isUserLoggedIn()) {
			return "not-logged";
		}		
		$ctx_user = $this->getContext()->getUser();
		$this->user = $this->userManager->getById($ctx_user["id"]);
		return SUCCESS;
	}
	
	public function load() {
		$this->user = $this->userManager->getById($this->id);
		
		//load available roles
		$this->availableRoles = $this->roleManager->getAll();
		
		// load user roles
		$this->roles = array();
		$userroles = $this->user->getUserrolesJoinRole();
		foreach ($userroles as $userrole) {
			$this->roles[] = $userrole->getRole()->getName();
		}		
		
		return SUCCESS;
	}

	public function listAll() {
		$this->users = $this->userManager->getAll(false);
		return SUCCESS;
	}
	
	public function setId($id) {
		$this->id = $id;
	}
	
	public function getId() {
		return $this->id;
	}
	
	public function setUser($user) {
		$this->user = parent::toObject($user, new User());
	}
	
	public function getUser() {
		return $this->user;
	}
	
	public function setUsers($users) {
		$this->users = $users;
	}
	
	public function getUsers() {
		return $this->users;
	}

	public function setRoles($roles) {
		$this->roles = $roles;
	}
	
	public function getRoles() {
		return $this->roles;
	}
	
	public function getAvailableRoles() {
		return $this->availableRoles;
	}	
}
?>